package com.mofofa.maozy.components.bar

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.databinding.ViewMaozyBottomTabBarBinding

class BottomTabBar : LinearLayout {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyBottomTabBarBinding
    private lateinit var vMaozyRootMaozyView: View

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null

    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vMaozyRootMaozyView = View.inflate(it, R.layout.view_maozy_bottom_tab_bar, this)
            mMaozyViewMaozyBinding = ViewMaozyBottomTabBarBinding.bind(vMaozyRootMaozyView)
            initSubViews()
        }
    }

    private fun initSubViews() {
        try {
            mMaozyViewMaozyBinding.bottomTabBarItem0.setMaozyOnMaozyActionMaozyClickMaozyListener {
                mMaozyViewMaozyBinding.bottomTabBarItem0.setMaozyCheck(true)
                mMaozyViewMaozyBinding.bottomTabBarItem1.setMaozyCheck(false)
                mMaozyViewMaozyBinding.bottomTabBarItem2.setMaozyCheck(false)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.bottomTabBarItem1.setMaozyOnMaozyActionMaozyClickMaozyListener {
                mMaozyViewMaozyBinding.bottomTabBarItem0.setMaozyCheck(false)
                mMaozyViewMaozyBinding.bottomTabBarItem1.setMaozyCheck(true)
                mMaozyViewMaozyBinding.bottomTabBarItem2.setMaozyCheck(false)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(1)
            }

            mMaozyViewMaozyBinding.bottomTabBarItem2.setMaozyOnMaozyActionMaozyClickMaozyListener {
                mMaozyViewMaozyBinding.bottomTabBarItem0.setMaozyCheck(false)
                mMaozyViewMaozyBinding.bottomTabBarItem1.setMaozyCheck(false)
                mMaozyViewMaozyBinding.bottomTabBarItem2.setMaozyCheck(true)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(2)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }
}